package com.spontaneappnew.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Typeface;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.RatingBar;
import android.widget.TextView;
import com.spontaneappnew.Comment;
import com.spontaneappnew.R;

public class VisitedPlacesAdapter extends BaseExpandableListAdapter {
	
	private LayoutInflater li;
	private ArrayList<Comment> places;

	
	public VisitedPlacesAdapter(Context context, ArrayList<Comment> PList){
		   this.places = PList;
		   this.li = LayoutInflater.from(context);
	}
	
	@Override
	public Comment getChild(int groupPosition, int childPosition) {
		return this.places.get(groupPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}
	


	@Override
	public View getChildView(int groupPosition, final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
		final Comment com = (Comment) getChild(groupPosition, childPosition);
		if (convertView == null) {
			convertView = li.inflate(R.layout.visited_places_row, null);
		}
		TextView comment = (TextView) convertView.findViewById(R.id.visited_place_row_comment);
		comment.setText(com.getComment());
		
		RatingBar rating = (RatingBar) convertView.findViewById(R.id.visited_place_row_rating);
		rating.setNumStars(5);
		rating.setRating((float)com.getRating());
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Comment getGroup(int groupPosition) {
		return this.places.get(groupPosition);
	}
	
	@Override
	public int getGroupCount() {
		if(places != null)
			return places.size();
		return 0; 
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
		String date = DateUtils.getRelativeTimeSpanString(getGroup(groupPosition).getDate().getTime()).toString();
		String name = getGroup(groupPosition).getPlaceName();
		
		if (convertView == null) {
				convertView = li.inflate(R.layout.visited_places_header, null);
		}
		
	    TextView headerDate = (TextView) convertView.findViewById(R.id.visited_place_header_date);
	    TextView headerName = (TextView) convertView.findViewById(R.id.visited_place_header_name);
	    headerName.setTypeface(null, Typeface.BOLD);
	    headerName.setText(name);
	    headerDate.setText(date);

	    return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
